package cn.com.dhcc.ddi.node.controller;

import java.util.Date;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.tag.CommonStaticCode.OPENCLOSE;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.app.pub.core.tag.staticcode.StaticCode;
import cn.com.dhcc.ddi.node.service.DataCategoryService;
import cn.com.dhcc.ddi.node.service.DomainInfoService;
import cn.com.dhcc.ddi.node.vo.DataCategory;
import cn.com.dhcc.ddi.node.vo.DomainInfo;

/**
 * 
    * @ClassName: DataCategory Controller
    * @Description: 数据分类Controller
    * @author 党向阳
    * @date 2016-11-24
    *
 */
@Controller
@RequestMapping("/node")
public class DataCategoryController extends BaseController {
	@Autowired
	private DataCategoryService dataCategoryService;
	
	@Autowired
	private DomainInfoService domainInfoService;
	
	private static Logger logger = Log.getLogger(LOGTYPE.DDI);
	/**
	 * 
	    * @Title: list
	    * @Description: 列表
	    * @param @param model
	    * @param @param pager
	    * @param @param name
	    * @param @param orderName
	    * @param @param order
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping(value = "/dataCategory_list")
	public String list(Model model, Pager pager, String name, String orderName, String order) {
		try {
			WhereCondition searchData = new WhereCondition();
			if (StrUtil.isNotBlank(name)) {
				searchData.where().like("t1.name", "%"+name+"%");
			}
			if (StrUtil.isNotBlank(orderName)) {
				searchData.orderBy(orderName + " " + order);
			} else {
				searchData.orderBy("mtime  desc");
			}
			PagerData<DataCategory> datas = dataCategoryService.DataCategorylist(pager, searchData);
			model.addAttribute(DATA_KEY, datas.getDatas());
			setTotoal(model, datas.getTotal());
			return "node/dataCategory/dataCategory_info_list";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}
	/**
	 * 
	    * @Title: get
	    * @Description: 获取对象
	    * @param @param model
	    * @param @param id
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping(value = "/dataCategory_get", method = RequestMethod.GET)
	public String get(Model model,String id) {
		try {
			DataCategory vo = dataCategoryService.get(id);
			DomainInfo domain = domainInfoService.getDomainInfo(vo.getDomain_id());
			vo.setDomName(domain.getName());
			model.addAttribute(DATA_KEY, vo);
		} catch (Exception e) {
			logger.error("获取数据分类信息失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "node/dataCategory/dataCategory_info_get";
	}
	
	/**
	 * 
	    * @Title: add
	    * @Description: 添加
	    * @param @param model
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	@RequestMapping("/dataCategory_add")
	public String add(Model model) {
		try {
			model.addAttribute("domList", domainInfoService.getDomain());
			return "node/dataCategory/dataCategory_info_add";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}
	

	@RequestMapping(value = "/add_dataCategory", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(Model model, DataCategory vo) {
		String msg = "";
		try {
			vo.setId(UUIDGenerator.getUUID());
			vo.setMtime(new Date());
			vo.setStatus(OPENCLOSE.OPEN.getStatus());
			dataCategoryService.insert(vo);
			msg = "添加成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "添加失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	
	/**
	 * 
	    * @Title: update
	    * @Description: 修改
	    * @param @param model
	    * @param @param id
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	 */
	
	@RequestMapping("/dataCategory_update")
	public String update(Model model, String id) {
		try {
			
			model.addAttribute("List", domainInfoService.getDomain());
			DataCategory dataCategory = dataCategoryService.get(id);
			model.addAttribute(VO_KEY, dataCategory);
			return "node/dataCategory/dataCategory_info_update";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}
	
	@RequestMapping(value = "/update_dataCategory", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(Model model, DataCategory vo) {
		String msg = "";
		try {
			
			vo.setMtime(new Date());
			dataCategoryService.update(vo);
			msg = "更新成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "更新失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	
	
	/**
	 * 根据ID删除记录
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/dataCategory_delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(String status,String id) {
		try {
			dataCategoryService.delete(status,id);
			return jsonSuccessResult("删除成功");
		} catch (Exception e) {
			logger.error("删除失败", e);
			return jsonFailResult("删除失败");
		}
	}
	
	
	
	/**
	 * 校验名称是否被使用
	 * @param name
	 * @param oldValue
	 * @return
	 */
	@RequestMapping("/dataCategory_check_name")
	@ResponseBody
	public String isNodeNameUnique(@RequestParam("name") String newValue,@RequestParam("oldValue") String oldValue) {
		try {
			
			if (dataCategoryService.isPropertyUnique("name", newValue, oldValue)) {
				return FORM_CHECK_SUCCESS;
			} else {
				return FORM_CHECK_FAIL;
			}
		} catch (Exception e) {
			return FORM_CHECK_FAIL;
		}
	}

	
	/**
	 * 物理删除
	    * @Title: lockNodeInfo
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @param id
	    * @param @param name
	    * @param @return    参数
	    * @return Map<String,Object>    返回类型
	    * @throws
	 */
	@RequestMapping(value="/lock_dataCategory_info")
	@ResponseBody
	public Map<String, Object> lockDataCategory(String id, String name){
		String status = StaticCode.DATA_STATUS.DISABLE.getCode();
		boolean isSuc = dataCategoryService.del(id, status);
		if (isSuc){
			return jsonSuccessResult("停用成功");
		}
		return jsonFailResult("停用失败");
	}
	
	@RequestMapping(value="/unlock_dataCategory_info")
	@ResponseBody
	public Map<String, Object> unlockDataCategory(String id, String name){
		String status = StaticCode.DATA_STATUS.ENABLE.getCode();
		boolean isSuc = dataCategoryService.del(id, status);
		if (isSuc){
			return jsonSuccessResult("启用成功");
		}
		return jsonFailResult("启用失败");
	}
}
